package com.example.busshop.service;

import com.example.busshop.common.service.LogicService;
import com.example.busshop.dao.*;
import com.example.busshop.entity.*;
import org.aspectj.weaver.ast.Or;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;

@Service
public class CustomerService extends LogicService<CustomerDao, Customer,Long> {
    @Resource
    OrderDao orderDao;
    @Resource
    ShopDao shopDao;
    @Resource
    CustomerDao customerDao;
    @Resource
    LineItemDao lineItemDao;
    public CustomerService(@Autowired CustomerDao dao) {
        super(dao);
    }
    public Order createOrder(Long cid, Long shopId,Set<LineItem> lineItems){
        Customer customer=customerDao.getReferenceById(cid);
        Shop shop=shopDao.getReferenceById(shopId);
        Order order=customer.createOrder();
        order.setShop(shop);
        order.setCustomer(customer);
        orderDao.save(order);
        order.setLineItems(lineItems);
        return order;
    }
    public int payOrder(Long oid){
        return orderDao.pay(oid);
    }
    public int cancel(Long oid){
        return orderDao.IsDeleted(oid);
    }
    public List<Order> allOrder(Long cid){
        return orderDao.listOrder(cid);
    }
    public Order chooseItem(Long oid,Item item,double quantity){
        Order order=orderDao.getReferenceById(oid);
        Set<LineItem>lineItems=order.addItem(item,quantity);
        order.setLineItems(lineItems);
        return order;
    }
    public Set<Order> MyallOrder(Long cid){
        Customer customer=getDAO().getReferenceById(cid);
        return customer.getOrders();
    }
}
